//引入mysql模块
const mysql=require('mysql');
/*
//创建连接对象
const c=mysql.createConnection({
  host:'127.0.0.1',
  port:'3306',
  user:'root',
  password:'',
  database:'tedu'
});
//测试连接
c.connect();
*/

//创建连接池对象
const pool=mysql.createPool({
  host:'127.0.0.1',
  port:'3306',
  user:'root',
  password:'',
  database:'tedu',
  connectionLimit:15
});
/*
//执行SQL命令,会自动获取连接
pool.query('select * from emp',(err,result)=>{
  //err可能产生的错误结果
  if(err){
    throw err;
  }
  //result成功的结果
  console.log(result);
});


//获取了传递的姓名
var str='xxx" or "1';
//'select * from emp where ename="'+'xxx" or "1'+'"'
//select * from emp where ename="xxx" or "1"
pool.query('select * from emp where ename="'+str+'"',(err,result)=>{
  //err可能产生的错误结果
  if(err){
    throw err;
  }
  //result成功的结果
  console.log(result);
});


//获取了传递的姓名
var str='xxx" or "1';
var sex=0;
pool.query('select * from emp where ename=? or sex=?',[str,sex],(err,result)=>{
  //err可能产生的错误结果
  if(err){
    throw err;
  }
  //result成功的结果
  console.log(result);
});

//练习：假设获取了传递一个员工数据，格式为对象，包含(姓名、性别、生日、工资、部门编号)，执行SQL命令，将数据插入到数据表emp
var obj={
  ename:'涛桑',
  sex:1,
  birthday:'1973-7-15',
  salary:50000,
  deptId:30
}
pool.query('insert into emp values(null,?,?,?,?,?)',[obj.ename,obj.sex,obj.birthday,obj.salary,obj.deptId],(err,result)=>{
  if(err) throw err;
  console.log(result);
});

var obj={
  salary:50000,
  //sex:1,
  //birthday:'1973-7-15',
  ename:'涛桑22',
  deptId:30
}
pool.query('insert into emp set?',[obj],(err,result)=>{
  if(err) throw err;
  console.log(result);
});
*/
//练习：声明变量保存任意一个编号，删除编号对应的员工
var n=15;
pool.query('delete from emp where eid=?',[n],(err,result)=>{
  if(err) throw err;
  console.log(result);
});



